package mydictionary_;

import java.io.*;

/**
 * @author Administrator
 * 根据实现类：文件实现
 */
public class DictionaryDaoIO implements DictionaryDao {

  @Override
  public void save(Mydictionary myDictionary) {
//把传入的参数myDictioary对象写入文件 d:\\d.txt
    //字符缓冲流一次写一行
    BufferedWriter bw = null;
    try {
      //可以追加写入文件
      bw = new BufferedWriter(new FileWriter("d:\\d.txt", true));
      bw.write(myDictionary.getId() + "--" + myDictionary.getEnglish() + "--"
          + myDictionary.getChinse() + "--" + myDictionary.getComments());
      bw.newLine();
      bw.flush();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (bw != null) {
        try {
          bw.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

  }

  @Override
  public Mydictionary query(String name) {
        /*根据传入的英文name，返回词典对象
        * 循环去读取文件中的每一行文本
        * 比较（name & 文本）
        * 相同：返回对象
        * */
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader("d:\\d.txt"));
      String line;
      while ((line = br.readLine()) != null) {
        String[] data = line.split("--");
        if (name.equals(data[1])) {
          int id = Integer.parseInt(data[0]);
          String english = data[1];
          String chinese = data[2];
          String comments = data[3];
          Mydictionary d = new Mydictionary(id, english, chinese, comments);
          return d;

        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return null;
  }


  @Override
  public void queryAll() {
    BufferedReader br = null;
    try {
      br = new BufferedReader(new FileReader("D:\\d.txt"));
      String line = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }


  @Override
  public void delete(String name) {
    if (query(name) == null) {
/*：
   判断要删除的内容是否纯在
   若不存在提示不存在
   若存在
   循环操作：
   读取源文件中的每一行文本
   比较是否要删除的内容
   若不是，写入新文件
   最后，将源文件删除，新文件重命名
**/
      System.out.println("要删除的内容不存在");
      //结束delete方法
      return;
    } else {
    BufferedWriter bw = null;
    BufferedReader br = null;
    try{
      br=new  BufferedReader(new FileReader("d:\\d.txt"));
       bw=new BufferedWriter(new FileWriter("d:\\new.txt",true));
       String line;
       while ((line=br.readLine())!=null){
         String[] data=line.split("--");
         if(!name.equals(data[1]) ){
          bw.write(line);
          bw.newLine();
          bw.flush();
         }
       }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }finally {
      if(br!=null){
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if(bw!=null){
        try{
          bw.close();
        }catch (IOException e){
          e.printStackTrace();
        }
      }
    }
    }
    File oldFile=new File("d:\\d.txt");
    File  newfile=new File("d:\\new.txt");
    oldFile.delete();
    newfile.renameTo(oldFile);
    System.out.println("删除成功");
      }
    }




